<?php

namespace app\admin\controller\buchang;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\response\Json;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Chakan extends Backend
{

    /**
     * Chakan模型对象
     * @var \app\admin\model\buchang\Chakan
     */
    protected $model = null;
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\buchang\Chakan;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->site = Config::get('site');
        $this->view->assign('chexing', $this->site['chexing']);
        $this->view->assign('xingzhi', $this->site['xingzhi']);
        $this->view->assign('task', $this->site['task_list']);
        $this->view->assign('site', $this->site);
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
//        $where1['baoan_status'] = ['<>', 1];
        $list = $this->model
            ->where($where)
            ->with(['zuzhi'])
//            ->where($where1)
            ->order($sort, $order)
            ->paginate($limit);
        $admin = $this->auth->getUserInfo();
        foreach ($list as &$v) {
            $v['ben_sun'] = "0";
            if ($v['status'] == 5) {
                $v['ben_sun'] = "1";
            }
            $v['san_cai'] = Db::name("scwusun")->where("bid", $v['id'])->where("status", "5")->count();
            $v['san_che'] = Db::name("sanche")->where("bid", $v['id'])->where("status", "5")->count();
            $v['ben_ren'] = Db::name("benren")->where("bid", $v['id'])->where("status", "5")->count();
            $v['san_ren'] = Db::name("sanren")->where("bid", $v['id'])->where("status", "5")->count();
            $v['luru'] = 0;
            if ($admin['id'] == $v['chakan_user_id'] || $admin['id'] == 1) {
                $v['luru'] = 1;//可以录入
            }
            //查询是否有退的单子
            $tuihui = '';
            $tui1 = Db::name("baoan")->where("id", $v['id'])->where("tui", 2)->count();
            if ($tui1) {
                $tuihui .= '【本车定损】';
            }
            $tui2 = Db::name("sanche")->where("bid", $v['id'])->where("tui", 2)->count();
            if ($tui2) {
                $tuihui .= '【三者车损】';
            }
            $tui3 = Db::name("scwusun")->where("bid", $v['id'])->where("tui", 2)->count();

            if ($tui3) {
                $tuihui .= '【三者财物损失】';
            }
            $tui4 = Db::name("benren")->where("bid", $v['id'])->where("tui", 2)->count();
            if ($tui4) {
                $tuihui .= '【本车人伤】';
            }
            $tui5 = Db::name("sanren")->where("bid", $v['id'])->where("tui", 2)->count();
            if ($tui5) {
                $tuihui .= '【三者人伤】';
            }
            $v['tui'] = $tuihui;
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function luru($ids)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $row['yw_class_text'] = Db::name('zuzhi')->where('id', $row['yw_class'])->value('name');
            $row['chakan_group_name'] = Db::name('zuzhi')->where('id', $row['chakan_group'])->value('name');
            $this->view->assign('row', $row);
            $res = Db::name('list_one')->where('tong_number', $row['tong_number'])
                ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id,dianwang,dw_tong,dw_biao,dw_zhe,dw_ying,dw_ji')
                ->find();
            $res['yw_class_text'] = $row['yw_class_text'];
            $this->view->assign('res', $res);
            $this->view->assign('id', $ids);
//            $file = Db::name('files')->where('bid',$ids)->column('url');
//            $this->assign('files',implode(',',$file));
            $task = [];//委派任务
            $admin = $this->auth->getUserInfo();
            if ($admin['id'] == 1) {
                $task = array_keys($this->site['task_list']);
            } else {
                $task = Db::name('diaodu_log')->where('bid', $ids)->order('id desc')->value('task');
                if (!empty($task)) $task = explode(',', $task);
            }
            $this->view->assign('task', $task);
            //三者车损
            $sanche = Db::name('sanche')->where('bid', $ids)->select();
            $this->assign('sanche', $sanche);
            $this->assign('reciver', []);
            //三者财物损失
            $scwusun = Db::name('scwusun')->where('bid', $ids)->select();
            foreach ($scwusun as $index => $item) {
                if (empty($item['c_zongji'])) {
                    $scwusun[$index]['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $scwusun[$index]['dong_money'] = $item['c_zongji'];
                }

            }
            $this->assign('scwusun', $scwusun);
            //本车人伤
            $benren = Db::name('benren')->where('bid', $ids)->select();
            $this->assign('benren', $benren);
            //三者人伤
            $sanren = Db::name('sanren')->where('bid', $ids)->select();
            $this->assign('sanren', $sanren);
            //本车车损配件
            $peijian = Db::name('peijian')->where('bid', $ids)->select();
            $this->assign('peijian', $peijian);
            //本车工时
            $bengong = Db::name('bengong')->where('bid', $ids)->select();
            $this->assign('bengong', $bengong);
            $lisuan = Db::name('lisuan')->where('bid', $ids)->where("sid", $ids)->find();
            if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
            $this->view->assign('lisuan', $lisuan);
            $shoukuan = Db::name('shoukuan')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('shoukuan', $shoukuan);
            $qita = Db::name('qita')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('qita', $qita);
            $this->view->assign('sid', $row['id']);
            $this->view->assign('bid', $row['id']);
            $this->view->assign('type', 0);
            //审核意见
            $yijian = Db::name('byijian')->where('bid', $ids)->select();
            $this->assign('yijian', $yijian);
            //统计数据
            if($res['yw_belonger']){
                $one_tong = Db::name('list_one')->where('yw_belonger',$res['yw_belonger'])->where('status',5)->field('count(*) as one_num,sum(main_free) as one_money')->find();
                $one_ids = Db::name('list_one')->where('yw_belonger',$res['yw_belonger'])->where('status',5)->column('id');
                $baoan_tong = Db::name('baoan')->whereIn('one_id',$one_ids)->field('count(*) as baoan_num,sum(buchang_money) as baoan_money')->find();
                $tongji = array_merge($one_tong,$baoan_tong);
            }else{
                $tongji['one_num'] = 0;
                $tongji['one_money'] = 0;
                $tongji['baoan_num'] = 0;
                $tongji['baoan_money'] = 0;
            }
            $this->assign('tongji',$tongji);
            $this->assign('lisuan_status',0);
            return $this->view->fetch();
        }
    }

    /**
     * 录入修改
     * @return void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function luruadd()
    {
        if (true === $this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $params = $this->preExcludeFields($params);
            $id = $params['id'];
            unset($params['id']);
            $params['baoan_time'] = strtotime($params['baoan_time']);
            $params['chuxian_time'] = strtotime($params['chuxian_time']);
            $params['chakan_time'] = strtotime($params['chakan_time']);
            Db::startTrans();
            try {
                Db::name('baoan')->where('id', $id)->update($params);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
    }

    /**
     * 获取文件类型
     * @return void
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function file_class()
    {
        $id = input('post.luru_id');
        $data = Db::name('file_class')->where('orderid', 'in', [0, $id])->field('id,title as text,pid as parent,status,orderid')->select();
        $data = $this->getlength($data, $id);
//        $class_data = $this->getTree($data);
//        print_R($class_data);exit;
        $this->success('', '', $data);
    }

    /**
     * 获取数量
     * @param $data
     * @param $orderid
     * @return array
     * @throws \think\Exception
     */
    public function getlength($data, $orderid)
    {
        $datas = array();
        foreach ($data as $key => $value) {
            $value['type'] = 'menu';
            if ($value['parent'] == 0) {
                $arrid = DB::name('file_class')->where('pid', $value['id'])->column('id');
                $arrid[] = $value['id'];
                $length = Db::name('files')->where('bid', $orderid)->where('class_id', 'in', $arrid)->count();
                $value['parent'] = '#';
            } else {
                $length = Db::name('files')->where('bid', $orderid)->where('class_id', $value['id'])->count();
            }
            $value['text'] = $value['text'] . '(' . $length . ')';
            $datas[$key] = $value;
        }
        return $datas;
    }

    /**
     * 处理上下级
     * @param $data
     * @param $pid
     * @return array
     */
    public function getTree($data, $pid = 0)
    {
        $tree = array();
        foreach ($data as $k => $v) {
            if ($v["parent"] == $pid) {
                unset($data[$k]);
                if (!empty($data)) {
                    $children = $this->getTree($data, $v["id"]);
                    if (!empty($children)) {
                        $v["children"] = $children;
                    }
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }

    /**
     * 修改图片分类
     * @return void
     * @throws DbException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function edit_class()
    {
        $params = input('post.');
        $res = Db::name('file_class')->where('id', $params['nodeId'])->update(['title' => $params['data']]);
        if ($res) {
            $data = Db::name('file_class')->where('id', $params['nodeId'])->field('id,title as text,pid as parent,status,orderid')->select();
            $data = $this->getlength($data, $params['id']);
            $this->success('', '', $data);
        } else {
            $this->error('数据未变更');
        }
    }

    /**
     * 根据分类查询图片
     * @return void
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_files()
    {
        $keyword = input('post.keyword', '', 'trim');
        $class_id = input('post.class_id', '', 'trim');
        $id = input('post.id', 0, 'intval');
        if (!$id) $this->success('', '', []);
        $where = [];
        $where['bid'] = $id;
        if (strlen($keyword) != 0) $where['title'] = ['like', '%' . $keyword . '%'];
        if ($class_id != '' && $class_id != 0) {
            $pids = DB::name('file_class')->where('pid', $class_id)->column('id');
            if ($pids) {
                $where['class_id'] = ['in', $pids];
            } else {
                $where['class_id'] = $class_id;
            }
        }
        $res = Db::name('files')->where($where)->select();
        if ($res) {
            $this->success('', '', $res);
        } else {
            $this->error('暂无图片');
        }
    }

    /**
     * 添加分类
     * @return Json
     * @throws DbException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add_files_class()
    {
        $params = input('post.');
        $data = Db::name('file_class')->where('title', $params['title'])->find();
        $length = Db::name('file_class')->where('orderid', $params['ids'])->where('title', 'like', '%' . $params['title'] . '%')->where('pid', 0)->count();
        if ($length > 0) {
            $num = $length + 2;
            $title = $params['title'] . $num;
        } else {
            $title = $params['title'] . '2';
        }
        Db::startTrans();
        try {
            $inserdata = array(
                'title' => $title,
                'pid' => 0,
                'status' => 1,
                'orderid' => $params['ids']
            );
            $id = Db::name('file_class')->insertGetId($inserdata);
            $list = Db::name('file_class')->where('pid', $data['id'])->select();
            $alldata = array();
            foreach ($list as $key => $value) {
                $alldata[$key] = array(
                    'title' => $value['title'],
                    'pid' => $id,
                    'status' => 1,
                    'orderid' => $params['ids']
                );
            }
            $res = Db::name('file_class')->insertAll($alldata);
            if ($res) {
                Db::commit();
                $this->success('');
            } else {
                Db::rollback();
                $this->error('添加失败');
            }
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 三者车损单条保存
     * @return Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function sanche_save()
    {
        $data = $_POST;
        if ($data['car_num'] == '') {
            $this->error('请添加数据');
        }
        if (empty($data['id']) || empty($data['bid'])) {
            $this->error('参数错误');
        }
        $res = Db::name("sanche")->where('id', $data['id'])->where("status", "0")->update($data);
        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('数据未变更');
        }
    }

    /**
     * 三者车损单条提交审核
     * @return Json
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function sanche_audit()
    {
        $data = $_POST;
        if (!$data['id']) {
            $this->error('参数错误');
        }
        $datas['status'] = "1";
        $datas['tui'] = 1;
        $res = Db::name("sanche")->where('id', $data['id'])->where("status", "0")->find();
        if ($res) {
            $kuai = Db::name('lisuan')->where('bid', $res['bid'])->where("sid", $res['id'])->value('kuai');
            if ($kuai == 1) {
                $datas['status'] = "4";
            }
        } else {
            $this->error('请先保存');
        }
        $res = Db::name("sanche")->where('id', $data['id'])->where("status", "0")->update($datas);
        if ($res) {
            $this->success('提交审核成功');
        } else {
            $this->error('提交审核失败');
        }
    }

    /**
     * 三者车配置
     * @return string
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sancheluru()
    {
        $id = input('get.id', 0);
        $type = input('get.type', 0);
        $this->view->assign('type', $type);
        $sanche = Db::name('sanche')->where('id', $id)->find();
        $row = Db::name('baoan')->where('id', $sanche['bid'])->field('learned_name,learned_kind,learned_card,learned_phone')->find();
        $this->view->assign('row', $row);
        $sanche['hd_g_heji'] = bcadd($sanche['hd_p_zongji'], $sanche['hd_g_zongji'], 2);
        $sanche['hd_g_heji'] = bcadd($sanche['hd_g_heji'], $sanche['hd_g_shijiu'], 2);
        $sanpei = Db::name('sanpei')->where('sid', $id)->select();//配件
        $this->view->assign('sanpei', $sanpei);
        $sangong = Db::name('sangong')->where('sid', $id)->select();//工时
        $this->view->assign('sangong', $sangong);
        $he_price = 0;
        $he_price_gong = 0;
        foreach ($sanpei as $k => $v) {
            $he_price = bcadd($he_price, $v['price_he'], 2);
        }
        foreach ($sangong as $k1 => $v1) {
            $he_price_gong = bcadd($he_price_gong, $v1['price_he'], 2);
        }
        $sanche['he_price_pei'] = $he_price;
        $sanche['he_price_gong'] = $he_price_gong;
        $lisuan_status = 0;
        if($sanche['status'] != 0 && $sanche['status'] != 1 && $sanche['status'] != 2){
            $lisuan_status = 1;
        }
        $this->view->assign('lisuan_status', $lisuan_status);
        $this->view->assign('sanche', $sanche);
        $lisuan = Db::name('lisuan')->where('bid', $sanche['bid'])->where("sid", $sanche['id'])->find();
        if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
        $this->view->assign('lisuan', $lisuan);
        $shoukuan = Db::name('shoukuan')->where('bid', $sanche['bid'])->where('sid', $sanche['id'])->select();
        $this->view->assign('shoukuan', $shoukuan);
        $qita = Db::name('qita')->where('bid', $sanche['bid'])->where('sid', $sanche['id'])->select();
        $this->view->assign('qita', $qita);
        $this->view->assign('site', $this->site);

        $this->view->assign('sid', $sanche['id']);
        $this->view->assign('bid', $sanche['bid']);
        return $this->view->fetch();
    }

    /**
     * 三者车配件删除
     */
    public function canche_pei_delete()
    {
        $data = $_POST;
        $res = Db::name('sanpei')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 三者车配件添加
     */
//    public function peijian()
//    {
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['name'] = $data['name'][$i];
//                $date['fangan'] = $data['fangan'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['zhekou'] = '100';
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['sid'] = $sid;
//                $data_id = Db::name("sanpei")->where(['sid' => $date['sid'], 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("sanpei")->where('id', $data_id['id'])->update($date);
//                } else {
//                    Db::name("sanpei")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//    }
    public function peijian()
    {
        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        Db::startTrans();
        try {
            Db::name("sanpei")->where('sid',$sid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['name'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['name'] = $data['name'][$i];
                    $date[$i]['fangan'] = $data['fangan'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['zhekou'] = '100';
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("sanpei")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 三者工时删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function sanche_pei_del()
    {
        $data = $_POST;
        $res = Db::name('sangong')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 三者工时添加
     * @return void
     */
//    public function save_gong()
//    {
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['name'] = $data['name'][$i];
//                $date['fangan'] = $data['fangan'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['zhekou'] = '100';
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['sid'] = $sid;
//                $data_id = Db::name("sangong")->where(['sid' => $date['sid'], 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("sangong")->where('id', $data_id['id'])->update($date);
//                } else {
//                    Db::name("sangong")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//    }
    public function save_gong()
    {
        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        Db::startTrans();
        try {
            Db::name("sangong")->where('sid',$sid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['name'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['name'] = $data['name'][$i];
                    $date[$i]['fangan'] = $data['fangan'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['zhekou'] = '100';
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("sangong")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 理算保存
     * @return void
     */
    public function lisuan_save()
    {
        $data = $this->request->post('row/a');
        Db::startTrans();
        try {
            $ress = Db::name('lisuan')->where('bid', $data['bid'])->where("sid", $data['sid'])->find();
            if ($ress) {
                $res = Db::name('lisuan')->where('bid', $data['bid'])->where("sid", $data['sid'])->update($data);
            } else {
                $res = Db::name('lisuan')->insert($data);
            }
            if ($res) {
//                $buchang_money = Db::name('baoan')->where('id', $data['bid'])->value('buchang_money');
//                $zongji = bcadd($buchang_money,$data['zongji'],2);
//                Db::name('baoan')->where('id', $data['bid'])->update(['buchang_money' => $zongji]);
//
//                $zongji = Db::name('lisuan')->where('bid', $data['bid'])->value('zongji');
//                Db::name('baoan')->where('id', $data['bid'])->update(['buchang_money' => $zongji]);
                Db::commit();
                $this->success('保存成功');
            } else {
                Db::rollback();
                $this->error('数据未变更');
            }
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 收款人信息保存
     * @return Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
//    public function shoukuan()
//    {
//
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        $bid = $data['bid'];
//        unset($data['bid']);
//
//        if ($data['shoukuan_ren'][0] == '') {
//            return json(['msg' => '请添加数据']);
//        }
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['shoukuan_ren']); $i++) {
//                $date['ren'] = $data['shoukuan_ren'][$i];
//                $date['kind'] = $data['shoukuan_kind'][$i];
//                $date['card'] = $data['shoukuan_card'][$i];
//                $date['phone'] = $data['shoukuan_phone'][$i];
//                $date['relation'] = $data['shoukuan_relation'][$i];
//                $date['bank'] = $data['shoukuan_bank'][$i];
//                $date['account'] = $data['shoukuan_account'][$i];
//                $date['money'] = $data['shoukuan_money'][$i];
//                $date['bid'] = $bid;
//                $date['sid'] = $sid;
//                if (isset($data['id'][$i]) && $data['id'][$i]) {
//                    Db::name("shoukuan")->where('id', $data['id'][$i])->update($date);
//                } else {
//                    Db::name("shoukuan")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//    }
    public function shoukuan()
    {

        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        $bid = $data['bid'];
        unset($data['bid']);

        if ($data['shoukuan_ren'][0] == '') {
            return json(['msg' => '请添加数据']);
        }
        Db::startTrans();
        try {
            Db::name("shoukuan")->where('bid',$bid)->where('sid',$sid)->delete();
            for ($i = 0; $i < count($data['shoukuan_ren']); $i++) {
                if($data['shoukuan_ren'][$i]){
                    $date[$i]['ren'] = $data['shoukuan_ren'][$i];
                    $date[$i]['kind'] = $data['shoukuan_kind'][$i];
                    $date[$i]['card'] = $data['shoukuan_card'][$i];
                    $date[$i]['phone'] = $data['shoukuan_phone'][$i];
                    $date[$i]['relation'] = $data['shoukuan_relation'][$i];
                    $date[$i]['bank'] = $data['shoukuan_bank'][$i];
                    $date[$i]['account'] = $data['shoukuan_account'][$i];
                    $date[$i]['money'] = $data['shoukuan_money'][$i];
                    $date[$i]['bid'] = $bid;
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("shoukuan")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 收款和其他删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function sanche_shoukuan_del()
    {
        $data = $_POST;
        $type = $data['type'];
        if ($type == 'qita') {
            $res = Db::name('qita')->where('id', 'in', $data['id'])->delete();
        } else {
            $res = Db::name('shoukuan')->where('id', 'in', $data['id'])->delete();
        }
        return $res;
    }

    /**
     * 其他保存
     * @return Json|void
     */
//    public function qita_save()
//    {
//
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        $bid = $data['bid'];
//        unset($data['bid']);
//
//        if ($data['qita_ren'][0] == '') {
//            return json(['msg' => '请添加数据']);
//        }
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['qita_ren']); $i++) {
//                $date['ren'] = $data['qita_ren'][$i];
//                $date['kind'] = $data['qita_kind'][$i];
//                $date['card'] = $data['qita_card'][$i];
//                $date['phone'] = $data['qita_phone'][$i];
//                $date['relation'] = $data['qita_relation'][$i];
//                $date['bank'] = $data['qita_bank'][$i];
//                $date['account'] = $data['qita_account'][$i];
//                $date['money'] = $data['qita_money'][$i];
//                $date['bid'] = $bid;
//                $date['sid'] = $sid;
//                if (isset($data['id'][$i]) && $data['id'][$i]) {
//                    Db::name("qita")->where('id', $data['id'][$i])->update($date);
//                } else {
//                    Db::name("qita")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//    }
    public function qita_save()
    {

        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        $bid = $data['bid'];
        unset($data['bid']);

        if ($data['qita_ren'][0] == '') {
            return json(['msg' => '请添加数据']);
        }
        Db::startTrans();
        try {
            Db::name("qita")->where(['sid'=>$sid,'bid'=>$bid])->delete();
            for ($i = 0; $i < count($data['qita_ren']); $i++) {
                if($data['qita_ren'][$i]){
                    $date[$i]['ren'] = $data['qita_ren'][$i];
                    $date[$i]['kind'] = $data['qita_kind'][$i];
                    $date[$i]['card'] = $data['qita_card'][$i];
                    $date[$i]['phone'] = $data['qita_phone'][$i];
                    $date[$i]['relation'] = $data['qita_relation'][$i];
                    $date[$i]['bank'] = $data['qita_bank'][$i];
                    $date[$i]['account'] = $data['qita_account'][$i];
                    $date[$i]['money'] = $data['qita_money'][$i];
                    $date[$i]['bid'] = $bid;
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("qita")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 三者车损最后保存
     * @return void
     */
    public function zuihou_save()
    {
        $gong = $this->request->post('gong/a');
        $pei = $this->request->post('pei/a');
        $row = $this->request->post('row/a');
        $sid = $this->request->post('sid');
        $bid = $this->request->post('bid');
        $data = array_merge($gong, $pei, $row);
        Db::startTrans();
        try {
            if ($data['kuai_shen']) {
                $ress = Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->find();
                if ($ress) {
                    Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->update(['kuai' => $data['kuai_shen']]);
                } else {
                    Db::name('lisuan')->insert(['kuai' => $data['kuai_shen'], 'sid' => $sid, 'bid' => $bid]);
                }
            }
            unset($data['kuai_shen']);
            $res = Db::name('sanche')->where('id', $sid)->update($data);
            $ress = Db::name('sanche')->where('bid', $bid)->field('xuhao,id,car_num,car_user,sex,phone,brand,model,style,car_vin,car_engine,weixiuchang,heji,c_zongji,sanche_huizong,status')->select();
            Db::commit();
            $this->success('', '', $ress);
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 三者财务损失保存单条
     * @return Json|void
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function sancai_one_save()
    {
        $data = $_POST;
        if ($data['name'] == '') {
            return json(['msg' => '名称不能为空']);
        }
        if (empty($data['id']) || empty($data['bid'])) {
            return json(['msg' => '参数错误']);
        }
        $res = Db::name("scwusun")->where('id', $data['id'])->update($data);
        if ($res) {
            $this->success('保存成功');
        }
        $this->error('数据未变更');
    }

    /**
     * 三者财务损失单条提交审核
     * @return Json
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function sancai_audit()
    {
        $data = $_POST;
        if (!$data['id']) {
            $this->error('参数错误');
        }
        $datas['status'] = "1";
        $datas['tui'] = 1;
        $res = Db::name("scwusun")->where('id', $data['id'])->where("status", "0")->find();
        if ($res) {
            $kuai = Db::name('lisuan')->where('bid', $res['bid'])->where("sid", $res['id'])->value('kuai');
            if ($kuai == 1) {
                $datas['status'] = "4";
            }
        } else {
            $this->error('请先保存');
        }
        $res = Db::name("scwusun")->where('id', $data['id'])->where("status", "0")->update($datas);
        if ($res) {
            $this->success('提交审核成功');
        } else {
            $this->error('提交审核失败');
        }
    }

    /**
     * 三者财务跳转录入
     * @return string
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sancailuru()
    {
        $id = input('get.id', 0);
        $type = input('get.type', 0);
        $this->view->assign('type', $type);
        $scwusun = Db::name('scwusun')->where('id', $id)->find();
        $this->view->assign('scwusun', $scwusun);
        $lisuan_status = 0;
        if($scwusun['status'] != 0 && $scwusun['status'] != 1 && $scwusun['status'] != 2){
            $lisuan_status = 1;
        }
        $this->view->assign('lisuan_status', $lisuan_status);
        $sancai = Db::name('sancai')->where('sid', $scwusun['id'])->select();
        $this->view->assign('sancai', $sancai);
        $lisuan = Db::name('lisuan')->where('bid', $scwusun['bid'])->where("sid", $scwusun['id'])->find();
        if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
        $this->view->assign('lisuan', $lisuan);
        $shoukuan = Db::name('shoukuan')->where('bid', $scwusun['bid'])->where('sid', $scwusun['id'])->select();
        $this->view->assign('shoukuan', $shoukuan);
        $qita = Db::name('qita')->where('bid', $scwusun['bid'])->where('sid', $scwusun['id'])->select();
        $this->view->assign('qita', $qita);
        $row = Db::name('baoan')->where('id', $scwusun['bid'])->field('learned_name,learned_kind,learned_card,learned_phone')->find();
        $this->view->assign('row', $row);
        $this->view->assign('sid', $scwusun['id']);
        $this->view->assign('bid', $scwusun['bid']);
        return $this->view->fetch();
    }

    /**
     * 三者财务删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function cancai_delete()
    {
        $data = $_POST;
        $res = Db::name('sancai')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 三者财务添加
     */
//    public function caiwu()
//    {
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['name'] = $data['name'][$i];
//                $date['fangan'] = $data['fangan'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['zhekou'] = '100';
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['sid'] = $sid;
//                $data_id = Db::name("sancai")->where(['sid' => $date['sid'], 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("sancai")->where('id', $data_id['id'])->update($date);
//                } else {
//                    Db::name("sancai")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//    }
    public function caiwu()
    {
        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        Db::startTrans();
        try {
            Db::name('sancai')->where('sid',$sid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['name'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['name'] = $data['name'][$i];
                    $date[$i]['fangan'] = $data['fangan'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['zhekou'] = '100';
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("sancai")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 三者财物损失最后保存
     * @return Json|void
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function sancai_save()
    {
        $data = $this->request->post('cai/a');
        $sid = $this->request->post('sid');
        $bid = $this->request->post('bid');

        Db::startTrans();
        try {
            if ($data['kuai_shen']) {
                $ress = Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->find();
                if ($ress) {
                    Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->update(['kuai' => $data['kuai_shen']]);
                } else {
                    Db::name('lisuan')->insert(['kuai' => $data['kuai_shen'], 'sid' => $sid, 'bid' => $bid]);
                }
            }
            unset($data['kuai_shen']);
            $res = Db::name('scwusun')->where('id', $sid)->update($data);
            $ress = Db::name('scwusun')->where('bid', $bid)->field('xuhao,id,name,fangan,c_money,hejia,status,c_zongji')->select();
            foreach ($ress as &$item) {
                if (empty($item['c_zongji'])) {
                    $item['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $item['dong_money'] = $item['c_zongji'];
                }
            }
            Db::commit();
            $this->success('', '', $ress);
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 本车人伤保存单条
     * @return Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function benren_save()
    {
        $data = $_POST;
        if ($data['name'] == '') {
            return json(['msg' => '姓名不能为空']);
        }
        if (empty($data['id']) || empty($data['bid'])) {
            return json(['msg' => '参数错误']);
        }
        $res = Db::name("benren")->where('id', $data['id'])->update($data);
        if ($res) {
            $this->success('保存成功');
        }
        $this->error('数据未变更');
    }

    /**
     * 本车人伤审核
     * @return Json
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function benren_audit()
    {
        $data = $_POST;
        if (empty($data['id'])) {
            return json(['msg' => '参数错误']);
        }
        $datas['status'] = "1";
        $datas['tui'] = 1;
        $res = Db::name("benren")->where('id', $data['id'])->where("status", "0")->find();
        if ($res) {
            $ress = Db::name('lisuan')->where('bid', $res['bid'])->where("sid", $res['id'])->value('kuai');
            if ($ress == 1) {
                $datas['status'] = "4";
            }
        }
        $res = Db::name("benren")->where('id', $data['id'])->where("status", "0")->update($datas);
        if ($res) $this->success('提交审核成功');
        $this->error('提交审核失败');
    }

    /**
     * 本车人伤跳转录入
     * @return void
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function benrenluru()
    {
        $id = input('get.id', 0);
        $type = input('get.type', 0);
        $this->view->assign('type', $type);
        $renshang = Db::name('renshang')->where('bid', $id)->select();
        $this->view->assign('renshang', $renshang);
        $benren = Db::name('benren')->where('id', $id)->find();
        $lisuan_status = 0;
        if($benren['status'] != 0 && $benren['status'] != 1 && $benren['status'] != 2){
            $lisuan_status = 1;
        }
        $this->view->assign('lisuan_status', $lisuan_status);
        $this->view->assign('benren', $benren);
        $lisuan = Db::name('lisuan')->where('bid', $benren['bid'])->where("sid", $benren['id'])->find();
        if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
        $this->view->assign('lisuan', $lisuan);
        $shoukuan = Db::name('shoukuan')->where('bid', $benren['bid'])->where('sid', $benren['id'])->select();
        $this->view->assign('shoukuan', $shoukuan);
        $qita = Db::name('qita')->where('bid', $benren['bid'])->where('sid', $benren['id'])->select();
        $this->view->assign('qita', $qita);
        $row = Db::name('baoan')->where('id', $benren['bid'])->field('learned_name,learned_kind,learned_card,learned_phone')->find();
        $this->view->assign('row', $row);
        $this->view->assign('sid', $benren['id']);
        $this->view->assign('bid', $benren['bid']);
        return $this->view->fetch();
    }

    /**
     * 本车人伤看病信息保存
     * @return void
     */
//    public function benren()
//    {
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['leixing'] = $data['leixing'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['bid'] = $sid;
//                $data_id = Db::name("renshang")->where(['bid' => $sid, 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("renshang")->where(['id' => $data_id['id']])->update($date);
//                } else {
//                    Db::name("renshang")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//    }
    public function benren()
    {
        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        Db::startTrans();
        try {
            Db::name("renshang")->where('bid',$sid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['type'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['leixing'] = $data['leixing'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['bid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("renshang")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 本车人伤删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException\
     */
    public function benren_delete()
    {
        $data = $_POST;
        $res = Db::name('renshang')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 本车人伤最后保存
     * @return void
     */
    public function zui_benren_save()
    {
        $data = $this->request->post('benren/a');
        $sid = $this->request->post('sid');
        $bid = $this->request->post('bid');
        Db::startTrans();
        try {
            if ($data['kuai_shen']) {
                $ress = Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->find();
                if ($ress) {
                    Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->update(['kuai' => $data['kuai_shen']]);
                } else {
                    Db::name('lisuan')->insert(['kuai' => $data['kuai_shen'], 'sid' => $sid, 'bid' => $bid]);
                }
            }
            unset($data['kuai_shen']);
            $res = Db::name('benren')->where('id', $sid)->update($data);
            $ress = Db::name('benren')->where('bid', $bid)->field('sex,id,type,name,age,quality,nuit,number,money,hejia,beizhu,status')->select();
            foreach ($ress as &$item) {
                if (empty($item['c_zongji'])) {
                    $item['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $item['dong_money'] = $item['c_zongji'];
                }
            }
            Db::commit();
            $this->success('', '', $ress);
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 三者人伤保存单条
     * @return Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function sanren_save()
    {
        $data = $_POST;
        if ($data['name'] == '') {
            return json(['msg' => '姓名不能为空']);
        }
        if (empty($data['id']) || empty($data['bid'])) {
            return json(['msg' => '参数错误']);
        }
        $res = Db::name("sanren")->where('id', $data['id'])->update($data);
        if ($res) {
            $this->success('保存成功');
        }
        $this->error('数据未变更');
    }

    /**
     * 三者人伤审核
     * @return Json
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\PDOException
     */
    public function sanren_audit()
    {
        $data = $_POST;
        if (empty($data['id'])) {
            return json(['msg' => '参数错误']);
        }
        $datas['status'] = "1";
        $datas['tui'] = 1;
        $res = Db::name("sanren")->where('id', $data['id'])->where("status", "0")->find();
        if ($res) {
            $ress = Db::name('lisuan')->where('bid', $res['bid'])->where("sid", $res['id'])->value('kuai');
            if ($ress == 1) {
                $datas['status'] = "4";
            }
        }
        $res = Db::name("sanren")->where('id', $data['id'])->where("status", "0")->update($datas);
        if ($res) $this->success('提交审核成功');
        $this->error('提交审核失败');
    }

    /**
     * 三者人伤跳转录入
     * @return void
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sanrenluru()
    {
        $id = input('get.id', 0);
        $type = input('get.type', 0);
        $this->view->assign('type', $type);
        $renshang = Db::name('renshang')->where('sid', $id)->select();
        $this->view->assign('renshang', $renshang);
        $sanren = Db::name('sanren')->where('id', $id)->find();
        $lisuan_status = 0;
        if($sanren['status'] != 0 && $sanren['status'] != 1 && $sanren['status'] != 2){
            $lisuan_status = 1;
        }
        $this->view->assign('lisuan_status', $lisuan_status);
        $this->view->assign('sanren', $sanren);
        $lisuan = Db::name('lisuan')->where('bid', $sanren['bid'])->where("sid", $sanren['id'])->find();
        if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
        $this->view->assign('lisuan', $lisuan);
        $shoukuan = Db::name('shoukuan')->where('bid', $sanren['bid'])->where('sid', $sanren['id'])->select();
        $this->view->assign('shoukuan', $shoukuan);
        $qita = Db::name('qita')->where('bid', $sanren['bid'])->where('sid', $sanren['id'])->select();
        $this->view->assign('qita', $qita);
        $row = Db::name('baoan')->where('id', $sanren['bid'])->field('learned_name,learned_kind,learned_card,learned_phone')->find();
        $this->view->assign('row', $row);
        $this->view->assign('sid', $sanren['id']);
        $this->view->assign('bid', $sanren['bid']);
        return $this->view->fetch();
    }

    /**
     * 三者人伤看病信息保存
     * @return void
     */
//    public function sanren()
//    {
//        $data = $this->request->post('row/a');
//        $sid = $data['sid'];
//        unset($data['sid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['leixing'] = $data['leixing'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['sid'] = $sid;
//                $data_id = Db::name("renshang")->where(['sid' => $sid, 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("renshang")->where(['id' => $data_id['id']])->update($date);
//                } else {
//                    Db::name("renshang")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//    }
    public function sanren()
    {
        $data = $this->request->post('row/a');
        $sid = $data['sid'];
        unset($data['sid']);
        Db::startTrans();
        try {
            Db::name("renshang")->where('sid',$sid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['type'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['leixing'] = $data['leixing'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['sid'] = $sid;
                }
            }
            if(isset($date) && $date){
                Db::name("renshang")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 三者人伤删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException\
     */
    public function sanren_delete()
    {
        $data = $_POST;
        $res = Db::name('renshang')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 三者人伤最后保存
     * @return void
     */
    public function zui_sanren_save()
    {
        $data = $this->request->post('sanren/a');
        $sid = $this->request->post('sid');
        $bid = $this->request->post('bid');
        Db::startTrans();
        try {
            if ($data['kuai_shen']) {
                $ress = Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->find();
                if ($ress) {
                    Db::name('lisuan')->where('bid', $bid)->where("sid", $sid)->update(['kuai' => $data['kuai_shen']]);
                } else {
                    Db::name('lisuan')->insert(['kuai' => $data['kuai_shen'], 'sid' => $sid, 'bid' => $bid]);
                }
            }
            unset($data['kuai_shen']);
            $res = Db::name('sanren')->where('id', $sid)->update($data);
            $ress = Db::name('sanren')->where('bid', $bid)->field('sex,id,type,name,age,quality,nuit,number,money,hejia,beizhu,status')->select();
            foreach ($ress as &$item) {
                if (empty($item['c_zongji'])) {
                    $item['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $item['dong_money'] = $item['c_zongji'];
                }
            }
            Db::commit();
            $this->success('', '', $ress);
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 本车配件删除
     */
    public function benche_pei_delete()
    {
        $data = $_POST;
        $res = Db::name('peijian')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 本车配件添加
     */
//    public function ben_peijian()
//    {
//        $data = $this->request->post('row/a');
//        $bid = $data['bid'];
//        unset($data['bid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['name'] = $data['name'][$i];
//                $date['fangan'] = $data['fangan'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['zhekou'] = '100';
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['bid'] = $bid;
//                $data_id = Db::name("peijian")->where(['bid' => $date['bid'], 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("peijian")->where('id', $data_id['id'])->update($date);
//                } else {
//                    Db::name("peijian")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功', '');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//    }
    public function ben_peijian()
    {
        $data = $this->request->post('row/a');
        $bid = $data['bid'];
        unset($data['bid']);
        Db::startTrans();
        try {
            Db::name("peijian")->where('bid',$bid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['name'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['name'] = $data['name'][$i];
                    $date[$i]['fangan'] = $data['fangan'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['zhekou'] = '100';
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['bid'] = $bid;
                }
            }
            if(isset($date) && $date){
                Db::name("peijian")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功', '');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 本车工时删除
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function benche_gong_del()
    {
        $data = $_POST;
        $res = Db::name('bengong')->where('id', 'in', $data['id'])->delete();
        return $res;
    }

    /**
     * 本车工时添加
     * @return void
     */
//    public function save_ben_gong()
//    {
//        $data = $this->request->post('row/a');
//        $bid = $data['bid'];
//        unset($data['bid']);
//        Db::startTrans();
//        try {
//            for ($i = 0; $i < count($data['xuhao']); $i++) {
//                $date['xuhao'] = $data['xuhao'][$i];
//                $date['type'] = $data['type'][$i];
//                $date['name'] = $data['name'][$i];
//                $date['fangan'] = $data['fangan'][$i];
//                $date['price'] = $data['price'][$i];
//                $date['num'] = $data['num'][$i];
//                $date['money'] = $data['money'][$i];
//                $date['hejia'] = $data['hejia'][$i];
//                $date['zhekou'] = '100';
//                $date['beizhu'] = $data['beizhu'][$i];
//                $date['bid'] = $bid;
//                $data_id = Db::name("bengong")->where(['bid' => $date['bid'], 'xuhao' => $data['xuhao'][$i]])->find();
//                if ($data_id) {
//                    Db::name("bengong")->where('id', $data_id['id'])->update($date);
//                } else {
//                    Db::name("bengong")->insert($date);
//                }
//            }
//            Db::commit();
//            $this->success('保存成功');
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//
//    }
    public function save_ben_gong()
    {
        $data = $this->request->post('row/a');
        $bid = $data['bid'];
        unset($data['bid']);
        Db::startTrans();
        try {
            Db::name("bengong")->where('bid',$bid)->delete();
            for ($i = 0; $i < count($data['xuhao']); $i++) {
                if($data['name'][$i]){
                    $date[$i]['xuhao'] = $data['xuhao'][$i];
                    $date[$i]['type'] = $data['type'][$i];
                    $date[$i]['name'] = $data['name'][$i];
                    $date[$i]['fangan'] = $data['fangan'][$i];
                    $date[$i]['price'] = $data['price'][$i];
                    $date[$i]['num'] = $data['num'][$i];
                    $date[$i]['money'] = $data['money'][$i];
                    $date[$i]['hejia'] = $data['hejia'][$i];
                    $date[$i]['zhekou'] = '100';
                    $date[$i]['beizhu'] = $data['beizhu'][$i];
                    $date[$i]['bid'] = $bid;
                }
            }
            if(isset($date) && $date){
                Db::name("bengong")->insertAll($date);
            }
            Db::commit();
            $this->success('保存成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    /**
     * 浏览
     * @param $ids
     * @return string|void
     * @throws DbException
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function details($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $row['yw_class_text'] = Db::name('zuzhi')->where('id', $row['yw_class'])->value('name');
            $row['chakan_group_name'] = Db::name('zuzhi')->where('id', $row['chakan_group'])->value('name');
            $this->view->assign('row', $row);
            $res = Db::name('list_one')->where('tong_number', $row['tong_number'])
                ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id,dianwang,dw_tong,dw_biao,dw_zhe,dw_ying,dw_ji')
                ->find();
            $res['yw_class_text'] = $row['yw_class_text'];
            $this->view->assign('res', $res);
            $this->view->assign('id', $ids);
            $task = [];//委派任务
            $admin = $this->auth->getUserInfo();
            if ($admin['id'] == 1) {
                $task = array_keys($this->site['task_list']);
            } else {
                $task = Db::name('diaodu_log')->where('bid', $ids)->order('id desc')->value('task');
                if (!empty($task)) $task = explode(',', $task);
            }
            $this->view->assign('task', $task);
            //三者车损
            $sanche = Db::name('sanche')->where('bid', $ids)->select();
            $this->assign('sanche', $sanche);
            $this->assign('reciver', []);
            //三者财物损失
            $scwusun = Db::name('scwusun')->where('bid', $ids)->select();
            foreach ($scwusun as $index => $item) {
                if (empty($item['c_zongji'])) {
                    $scwusun[$index]['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $scwusun[$index]['dong_money'] = $item['c_zongji'];
                }

            }
            $this->assign('scwusun', $scwusun);
            //本车人伤
            $benren = Db::name('benren')->where('bid', $ids)->select();
            $this->assign('benren', $benren);
            //三者人伤
            $sanren = Db::name('sanren')->where('bid', $ids)->select();
            $this->assign('sanren', $sanren);
            //本车车损配件
            $peijian = Db::name('peijian')->where('bid', $ids)->select();
            $this->assign('peijian', $peijian);
            //本车工时
            $bengong = Db::name('bengong')->where('bid', $ids)->select();
            $this->assign('bengong', $bengong);
            $lisuan = Db::name('lisuan')->where('bid', $ids)->where("sid", $ids)->find();
            if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
            $this->view->assign('lisuan', $lisuan);
            $shoukuan = Db::name('shoukuan')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('shoukuan', $shoukuan);
            $qita = Db::name('qita')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('qita', $qita);
            $this->view->assign('sid', $row['id']);
            $this->view->assign('bid', $row['id']);
            $this->view->assign('type', 1);
            //审核意见
            $yijian = Db::name('byijian')->where('bid', $ids)->select();
            $this->view->assign('yijian', $yijian);
            $lisuan_status = 0;
            if($row['status'] != 0 && $row['status'] != 1 && $row['status'] != 2){
                $lisuan_status = 1;
            }
            $this->view->assign('lisuan_status', $lisuan_status);
            return $this->view->fetch();
        }
    }

    /**
     * 保存备注
     * @return Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function ben_save_beizhu()
    {
        $id = $this->request->post("id", 0, "intval");
        $data['dong_beizhu'] = $this->request->post("ben_beizhu", "", "trim");
        if (empty($id)) {
            $this->error('参数错误');
        }
        $res = Db::name('baoan')->where('id', $id)->where("status", "0")->update($data);
        if ($res) {
            $this->success('保存成功');
        } else {
            $this->error('数据未变更');
        }
    }

    /**
     * 本车定损审核
     * @return void
     */
    public function benche_shenhe()
    {
        $data = $_POST;
        $id = $data['id'];
        $date['up_time'] = time();
        $date['chesun_1'] = 1;
        $date['chesun'] = 1;
        $date['tui'] = 1;
        Db::startTrans();
        try {
            Db::name('lisuan')->where(['sid' => $id, 'bid' => $id])->update(['kuai' => $data['kuai_shen']]);
            if ($data['kuai_shen'] == 1) {
                $date['status'] = 4;
            } else {
                $date['status'] = 1;
            }
            $res = Db::name('baoan')->where('id', $id)->update($date);
            if ($res) {
                Db::commit();
                $this->success('提交成功');
            } else {
                Db::rollback();
                $this->error('提交失败');
            }
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

    }

    public function delete_image()
    {
        $id = input('post.id', 0);
        if ($id) {
            Db::name('files')->where('id', $id)->delete();
        }
    }
    public function zhuxiao($ids)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $row['yw_class_text'] = Db::name('zuzhi')->where('id', $row['yw_class'])->value('name');
            $row['chakan_group_name'] = Db::name('zuzhi')->where('id', $row['chakan_group'])->value('name');
            $this->view->assign('row', $row);
            $res = Db::name('list_one')->where('tong_number', $row['tong_number'])
                ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id,dianwang,dw_tong,dw_biao,dw_zhe,dw_ying,dw_ji')
                ->find();
            $res['yw_class_text'] = $row['yw_class_text'];
            $this->view->assign('res', $res);
            $this->view->assign('id', $ids);
            $task = [];//委派任务
            $admin = $this->auth->getUserInfo();
            if ($admin['id'] == 1) {
                $task = array_keys($this->site['task_list']);
            } else {
                $task = Db::name('diaodu_log')->where('bid', $ids)->order('id desc')->value('task');
                if (!empty($task)) $task = explode(',', $task);
            }
            $this->view->assign('task', $task);
            //三者车损
            $sanche = Db::name('sanche')->where('bid', $ids)->select();
            $this->assign('sanche', $sanche);
            $this->assign('reciver', []);
            //三者财物损失
            $scwusun = Db::name('scwusun')->where('bid', $ids)->select();
            foreach ($scwusun as $index => $item) {
                if (empty($item['c_zongji'])) {
                    $scwusun[$index]['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $scwusun[$index]['dong_money'] = $item['c_zongji'];
                }

            }
            $this->assign('scwusun', $scwusun);
            //本车人伤
            $benren = Db::name('benren')->where('bid', $ids)->select();
            $this->assign('benren', $benren);
            //三者人伤
            $sanren = Db::name('sanren')->where('bid', $ids)->select();
            $this->assign('sanren', $sanren);
            //本车车损配件
            $peijian = Db::name('peijian')->where('bid', $ids)->select();
            $this->assign('peijian', $peijian);
            //本车工时
            $bengong = Db::name('bengong')->where('bid', $ids)->select();
            $this->assign('bengong', $bengong);
            $lisuan = Db::name('lisuan')->where('bid', $ids)->where("sid", $ids)->find();
            if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
            $this->view->assign('lisuan', $lisuan);
            $shoukuan = Db::name('shoukuan')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('shoukuan', $shoukuan);
            $qita = Db::name('qita')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('qita', $qita);
            $this->view->assign('sid', $row['id']);
            $this->view->assign('bid', $row['id']);
            $this->view->assign('type', 1);
            //审核意见
            $yijian = Db::name('byijian')->where('bid', $ids)->select();
            $this->view->assign('yijian', $yijian);

            $lisuan_status = 0;
            if($row['status'] != 0 && $row['status'] != 1 && $row['status'] != 2){
                $lisuan_status = 1;
            }
            $this->view->assign('lisuan_status', $lisuan_status);
            return $this->view->fetch();
        }
    }

    /**
     * 注销保存
     * @return void
     */
    public function zhuxiaoadd(){
        if (true === $this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $params = $this->preExcludeFields($params);
            $id = $params['id'];
            unset($params['id']);
            $data['status'] = 6;
            $data['zhuxiao_beizhu'] = $params['zhuxiao_beizhu'];
            Db::startTrans();
            try {
                Db::name('baoan')->where('id', $id)->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
    }

    public function jupei($ids)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $row['yw_class_text'] = Db::name('zuzhi')->where('id', $row['yw_class'])->value('name');
            $row['chakan_group_name'] = Db::name('zuzhi')->where('id', $row['chakan_group'])->value('name');
            $this->view->assign('row', $row);
            $res = Db::name('list_one')->where('tong_number', $row['tong_number'])
                ->field('id,ids,yw_come,yw_class,yw_peo,yw_peo_phone,yw_belonger,yw_handle,yw_join_time,yw_enter_peo,yw_hetongpeo,yw_hetong_time,yw_finance_peo,yw_shenhe_time,tong_storage,tong_number,tong_document,give_company,give_starttime,give_endtime,car_num,car_is_new,car_num_kind,haocolor,car_province,car_check,car_check_res,car_owner,car_card_kind,car_card,car_relation,car_address,car_vin,car_engine,car_factory_num,car_register,car_age,car_issue_time,car_is_transfer,car_buy,car_reall,car_use_nature,car_leixing,car_kind,car_carry_peo,car_carry_quality,car_curb_weight,car_displacement,car_power,car_lastyear,car_rank,plan_strattime,plan_endtime,main_name,main_money,main_standard,main_discount,main_submit,main_franchise,main_disan,main_santong,main_biao,main_zhe,main_yingjiao,main_ji,dao_name,dao_tong,dao_biao,dao_zhe,dao_ying,dao_ji,siji,siji_tong,siji_biao,siji_zhe,siji_ying,siji_ji,chengke,ck_money,chengke_zuo,ck_tong,ck_biao,ck_zhe,ck_ying,ck_ji,buji,bu_tong,bu_biao,bu_zhe,bu_ying,bu_buji,ziran,zi_tong,zi_biao,zi_zhe,zi_ying,zi_buji,fdjsunshi,fdj_tong,fdj_biao,fdj_zhe,fdj_ying,fdj_buji,zdxiuli,zd_tong,zd_biao,zd_zhe,zd_ying,zd_buji,huowu,hw_tong,hw_biao,hw_zhe,hw_ying,hw_buji,boli,bl_tong,bl_biao,bl_zhe,bl_ying,bl_buji,main_sum,main_sumdiscount,main_free,main_netrate,main_netcost,ginseng_name,ginseng_kind,ginseng_card,ginseng_phone,ginseng_email,ginseng_address,learned_name,learned_kind,learned_num,learned_phone,learned_email,learned_address,resolvent,special,invoice_kind,invoice_name,invoice_num,invoice_bank,bank_address,invoice_phone,beizhu,zhanghao,file,file_name,shuoming,shenhe_time,shenhe_peo,shenhe_yijian,status,statuss,pigai_type,pigai_numz,pigai_nump,pigai_piwen,pigai_ctime,pigai_pddzh,main_tongchoufee_heji,main_xiaoguan_bili,main_xiaoguan,main_yunying_bili,main_yunying,main_tongchoujingfee_heji,dong_fee,ncd,give_number,kehu_sumdiscount,chexing,chexing_ci,feiyong,jiafei,jiaofei,jiaofei_price,jiaofei_time,jiaofei_type,trade_no,out_trade_no,type,lururen,lururen_id,feiyong_type,feiyong_qita,jiafei_type,shui_lv1,shui_lv,ziran_c,fd_c,zd_c,huowu_c,boli_c,urls1,urls2,tcxm,status_type,qianzi_status,pdf,chezhu,tk_type,bjt,gs_type,zq_id,dianwang,dw_tong,dw_biao,dw_zhe,dw_ying,dw_ji')
                ->find();
            $res['yw_class_text'] = $row['yw_class_text'];
            $this->view->assign('res', $res);
            $this->view->assign('id', $ids);
            $task = [];//委派任务
            $admin = $this->auth->getUserInfo();
            if ($admin['id'] == 1) {
                $task = array_keys($this->site['task_list']);
            } else {
                $task = Db::name('diaodu_log')->where('bid', $ids)->order('id desc')->value('task');
                if (!empty($task)) $task = explode(',', $task);
            }
            $this->view->assign('task', $task);
            //三者车损
            $sanche = Db::name('sanche')->where('bid', $ids)->select();
            $this->assign('sanche', $sanche);
            $this->assign('reciver', []);
            //三者财物损失
            $scwusun = Db::name('scwusun')->where('bid', $ids)->select();
            foreach ($scwusun as $index => $item) {
                if (empty($item['c_zongji'])) {
                    $scwusun[$index]['dong_money'] = Db::name('sancai')->where("sid", $item['id'])->sum("money");
                } else {
                    $scwusun[$index]['dong_money'] = $item['c_zongji'];
                }

            }
            $this->assign('scwusun', $scwusun);
            //本车人伤
            $benren = Db::name('benren')->where('bid', $ids)->select();
            $this->assign('benren', $benren);
            //三者人伤
            $sanren = Db::name('sanren')->where('bid', $ids)->select();
            $this->assign('sanren', $sanren);
            //本车车损配件
            $peijian = Db::name('peijian')->where('bid', $ids)->select();
            $this->assign('peijian', $peijian);
            //本车工时
            $bengong = Db::name('bengong')->where('bid', $ids)->select();
            $this->assign('bengong', $bengong);
            $lisuan = Db::name('lisuan')->where('bid', $ids)->where("sid", $ids)->find();
            if (!isset($lisuan['kuai'])) $lisuan['kuai'] = 2;
            $this->view->assign('lisuan', $lisuan);
            $shoukuan = Db::name('shoukuan')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('shoukuan', $shoukuan);
            $qita = Db::name('qita')->where('bid', $ids)->where('sid', $ids)->select();
            $this->view->assign('qita', $qita);
            $this->view->assign('sid', $row['id']);
            $this->view->assign('bid', $row['id']);
            $this->view->assign('type', 1);
            //审核意见
            $yijian = Db::name('byijian')->where('bid', $ids)->select();
            $this->view->assign('yijian', $yijian);

            $lisuan_status = 0;
            if($row['status'] != 0 && $row['status'] != 1 && $row['status'] != 2){
                $lisuan_status = 1;
            }
            $this->view->assign('lisuan_status', $lisuan_status);
            return $this->view->fetch();
        }
    }
    /**
     * 注销保存
     * @return void
     */
    public function jupeiadd(){
        if (true === $this->request->isPost()) {
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $params = $this->preExcludeFields($params);
            $id = $params['id'];
            unset($params['id']);
            $data['status'] = 7;
            $data['jupei_beizhu'] = $params['jupei_beizhu'];
            Db::startTrans();
            try {
                Db::name('baoan')->where('id', $id)->update($data);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
    }
}

